<template>
	<div class="home-container">
		<el-row :gutter="15">
			<el-col :sm="3" class="mb15">
				<div class="home-card-item home-card-first">
					<div class="flex-margin flex">
						<img src="/images/police.jpg" />
						<div class="home-card-first-right ml15">
							<div class="flex-margin">
								<div class="home-card-first-right-title">
									{{ currentTime }}，{{ getUserInfos.userName === '' ? 'test' : getUserInfos.userName }}！
								</div>
								<div class="home-card-first-right-msg mt5">{{ userInfo.organName }}</div>
							</div>
						</div>
					</div>
				</div>
			</el-col>
			<el-col :sm="3" class="mb15" v-for="(v, k) in caseReport" :key="k">
				<div class="home-card-item home-card-item-box" :style="{ background: v.color }">
					<div class="home-card-item-flex">
						<div class="home-card-item-title pb3" style="cursor: pointer;" @click="goToPages(v.title,1)">{{ v.title }}</div>
						<div class="home-card-item-title-num pb6" style="cursor: pointer;" @click="goToPages(v.title,1)">{{ v.titleNum }}</div>
						<div class="home-card-item-tip pb3" style="cursor: pointer;" @click="goToPages(v.title,2)">{{ v.tip }}</div>
						<div class="home-card-item-tip-num" style="cursor: pointer;" @click="goToPages(v.title,2)">{{ v.tipNum }}</div>
					</div>
					<i :class="v.icon" :style="{ color: v.iconColor }"></i>
				</div>
			</el-col>
		</el-row>
		<el-row :gutter="15">
			<el-col :xs="24" :sm="14" :md="14" :lg="16" :xl="16" class="mb15">
				<el-card shadow="hover" header="月度物品流转情况">
					<div style="height: 200px" ref="homeLaboratoryRef"></div>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="10" :md="10" :lg="8" :xl="8">
				<el-card shadow="hover" header="环境监测">
					<div class="home-monitor">
						<div class="flex-warp">
							<div class="flex-warp-item" v-for="(v, k) in environmentList" :key="k">
								<div class="flex-warp-item-box">
									<i :class="v.icon" :style="{ color: v.iconColor }"></i>
									<span class="pl5">{{ v.label }}</span>
									<div class="mt10">{{ v.value }}</div>
								</div>
							</div>
						</div>
					</div>
				</el-card>
			</el-col>
		</el-row>
		<el-row :gutter="15">
			<el-col :xs="24" :sm="7" :md="7" :lg="9" :xl="9" class="home-dynamic-media">
				<el-card shadow="hover" header="物品类别统计" class="home-warning-card">
					<div style="height: 320px" ref="homeEviTypeRef"></div>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="7" :md="7" :lg="7" :xl="7" class="home-warning-media">
				<el-card shadow="hover" header="最新动态" class="home-warning-card">
					<div class="home-dynamic">
						<el-scrollbar>
							<div class="home-dynamic-item" v-for="(v, k) in activitiesList" :key="k">
								<div class="home-dynamic-item-left">
									<div class="home-dynamic-item-left-time1 mb5">{{ v.time1 }}</div>
									<div class="home-dynamic-item-left-time2">{{ v.time2 }}</div>
								</div>
								<div class="home-dynamic-item-line">
									<i class="iconfont icon-fangkuang"></i>
								</div>
								<div class="home-dynamic-item-right">
									<div class="home-dynamic-item-right-title mb5">
										<i class="el-icon-s-comment"></i>
										<span>{{ v.title }}</span>
									</div>
									<div class="home-dynamic-item-right-label">{{ v.label }}</div>
								</div>
							</div>
						</el-scrollbar>
					</div>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="10" :md="10" :lg="8" :xl="8" class="home-dynamic-media">
				<el-card shadow="hover" header="异常出库" class="home-warning-card">
					<el-table :data="tableData" style="width: 100%" stripe :row-class-name="tableRowClassName">
						<el-table-column prop="date" width="200px" label="时间"></el-table-column>
						<el-table-column prop="label" label="报警内容">
							<template #default="scope">
								{{ `${scope.row.evidenceNo}/${scope.row.evidenceName}` }}
							</template>
						</el-table-column>
					</el-table>
				</el-card>
			</el-col>
		</el-row>
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, nextTick, computed, getCurrentInstance, watch, onActivated, ref } from 'vue';
import * as echarts from 'echarts';
import { CountUp } from 'countup.js';
import { formatAxis } from '/@/utils/formatTime';
import { useStore } from '/@/store/index'; 
import { useRouter } from 'vue-router';
import { getUserInfo, getCaseHome, getEvidenceHome, GetEvidenceClass, GetTransfereEvidenceTrends, GetWarnCheckout , GetEnvironmentList } from '/@/api/home';
import { stat } from 'fs';
export default {
	name: 'home',
	setup() {
		const { proxy } = getCurrentInstance() as any;
		const store = useStore();
		const state = reactive({
			environmentList:[],
			activitiesList: [],
			tableData: [],
			tableWarnRow:{index:0,dirt:'up'},
			myCharts: [],
			eviTypeCharts: [],
			userInfo: {},
			caseReport: [],
			evidenceReport: {},
			evidenceTrends: [],
			collectionStatus: false,
		});

		const router = useRouter();

		// 获取用户信息 vuex
		const getUserInfos = computed(() => {
			return store.state.userInfos.userInfos;
		});
		// 当前时间提示语
		const currentTime = computed(() => {
			return formatAxis(new Date());
		});
		setInterval(function(){
			if( state.tableData.length == 0)
				return;
			if(state.tableWarnRow.index == state.tableData.length - 1){
				state.tableWarnRow.dirt = 'down';
			}else if(state.tableWarnRow.index == 0){
				state.tableWarnRow.dirt = 'up';
			}
			state.tableWarnRow.index += (state.tableWarnRow.dirt == 'up'?1:-1);
		},2000);
		// 初始化数字滚动
		const initNumCountUp = () => {
			nextTick(() => {
				new CountUp('titleNum1', Math.random() * 10000).start();
				new CountUp('titleNum2', Math.random() * 10000).start();
				new CountUp('titleNum3', Math.random() * 10000).start();
				new CountUp('titleNum4', Math.random() * 10000).start();
				new CountUp('titleNum5', Math.random() * 10000).start();
				new CountUp('titleNum6', Math.random() * 10000).start();
				new CountUp('titleNum7', Math.random() * 10000).start();
				new CountUp('titleNum8', Math.random() * 10000).start();
				new CountUp('tipNum1', Math.random() * 1000).start();
				new CountUp('tipNum2', Math.random() * 1000).start();
				new CountUp('tipNum3', Math.random() * 1000).start();
				new CountUp('tipNum4', Math.random() * 1000).start();
				new CountUp('tipNum5', Math.random() * 1000).start();
				new CountUp('tipNum6', Math.random() * 1000).start();
				new CountUp('tipNum7', Math.random() * 1000).start();
				new CountUp('tipNum8', Math.random() * 1000).start();
			});
		};

		const initHomeEviType = (_data: any) => {
			const myChart = echarts.init(proxy.$refs.homeEviTypeRef);
			const option = {
				title: {
					left: 'center',
				},
				tooltip: {
					trigger: 'item',
				},
				
				series: [
					{
						name: '物品类别',
						type: 'pie',
						radius: '75%',
						data: _data,
						emphasis: {
							itemStyle: {
								shadowBlur: 10,
								shadowOffsetX: 0,
								shadowColor: 'rgba(0, 0, 0, 0.5)',
							},
						},
					},
				],
			};
			myChart.setOption(option);
			state.eviTypeCharts.push(myChart);
		};

	
		const goToPages = (title:String,type:Number) => {
			var _vUrl="/query/evidence";
			var _query={ty:type,vstatus:0,a:new Date().getTime()};
			// 借调 1  检验 2  发还 3  销毁 4 移交 5 归还 6  更换位置7  入库8
			switch(title)
			{
				case "案件总数":
				_vUrl="/query/case";
				break;
				case "物品总数":
				break;
				case "已入库":
				_query={ty:type,vstatus:8,a:new Date().getTime()};
				break;
				case "已借调":
				_query={ty:type,vstatus:1,a:new Date().getTime()};
				break;
				case "已发还":
				_query={ty:type,vstatus:3,a:new Date().getTime()};
				break;
				case "已移交":
				_query={ty:type,vstatus:5,a:new Date().getTime()};
				break;
				case "已回库":
				_query={ty:type,vstatus:6,a:new Date().getTime()};
				break;
			}

			
			router.push({
				path: _vUrl,
				query: _query,
			});
		};


		// 月度物品流转情况
		const initHomeLaboratory = (_data: any) => {
			const myChart = echarts.init(proxy.$refs.homeLaboratoryRef);
			const option = {
				grid: {
					top: 50,
					right: 20,
					bottom: 30,
					left: 30,
				},
				tooltip: {
					trigger: 'axis',
				},
				legend: {
					data: _data.names,
					right: 13,
				},

				xAxis: {
					data: _data.months,
				},
				yAxis: [
					{
						type: 'value',
						name: '数量',
					},
				],
				series: [
					{
						name: '已入库',
						type: 'bar',
						data: _data.list[0].list,
						itemStyle: {
							borderRadius: [4, 4, 0, 0],
							color: {
								x: 0,
								y: 0,
								x2: 0,
								y2: 1,
								type: 'linear',
								global: false,
								colorStops: [
									{
										offset: 0,
										color: '#0b9eff',
									},
									{
										offset: 1,
										color: '#63caff',
									},
								],
							},
						},
					},
					{
						name: '已出库',
						type: 'bar',
						data: _data.list[1].list,
						itemStyle: {
							borderRadius: [4, 4, 0, 0],
							color: {
								x: 0,
								y: 0,
								x2: 0,
								y2: 1,
								type: 'linear',
								global: false,
								colorStops: [
									{
										offset: 0,
										color: '#03387a',
									},
									{
										offset: 1,
										color: '#6c93ee',
									},
								],
							},
						},
					},
					{
						name: '已借调',
						type: 'line',
						data: _data.list[2].list,
						itemStyle: {
							borderRadius: [4, 4, 0, 0],
							color: '#ccaf27',
						},
					},
					{
						name: '已移交',
						type: 'line',
						data: _data.list[3].list,
						itemStyle: {
							borderRadius: [4, 4, 0, 0],
							color: '#f95b5a',
						},
					},
					// {
					// 	name: '已送检',
					// 	type: 'line',
					// 	data: _data.list[4].list,
					// 	itemStyle: {
					// 		borderRadius: [4, 4, 0, 0],
					// 		color: '#38b99c',
					// 	},
					// },
				],
			};
			myChart.setOption(option);
			state.myCharts.push(myChart);
		};

		// 批量设置 echarts resize
		const initEchartsResizeFun = () => {
			nextTick(() => {
				for (let i = 0; i < state.myCharts.length; i++) {
					state.myCharts[i].resize();
				}

				for (let i = 0; i < state.eviTypeCharts.length; i++) {
					state.eviTypeCharts[i].resize();
				}
			});
		};
		// 批量设置 echarts resize
		const initEchartsResize = () => {
			window.addEventListener('resize', initEchartsResizeFun);
		};
		const tableRowClassName = ({
			row,
			rowIndex,
		}) => {
			var _css = "";
			if (rowIndex < state.tableWarnRow.index) {
				return 'row-hide'
			}
			_css +=" warning-row1";
			return _css
		}

		// 页面加载时
		onMounted(() => {
			initNumCountUp();
			initEchartsResize();
		});
		// 由于页面缓存原因，keep-alive
		onActivated(() => {
			initEchartsResizeFun();
		});
		// 监听 vuex 中的 tagsview 开启全屏变化，重新 resize 图表，防止不出现/大小不变等
		watch(
			() => store.state.tagsViewRoutes.isTagsViewCurrenFull,
			() => {
				initEchartsResizeFun();
			}
		);
		function initHomeCase() {
			state.caseReport = [];
			getCaseHome().then((res: any) => {
				if (res.success) {
					state.caseReport = res.data.list;
				}
			});
		}
		function initUser() {
			getUserInfo().then((res: any) => {
				if (res.success) {
					state.userInfo = res.data;
				}
			});
		}
		function initHomeEvidence() {
			getEvidenceHome().then((res: any) => {
				if (res.success) {
					state.evidenceReport = res.data;
					initHomeLaboratory(res.data);
				}
			});
		}
		function initClass() {
			GetEvidenceClass().then((res: any) => {
				if (res.success) {
					initHomeEviType(res.data.list);
				}
			});
		}
		function initTrends() {
			GetTransfereEvidenceTrends().then((res: any) => {
				if (res.success) {
					state.activitiesList = res.data.list;
				}
			});
		}
		function initWarning() {
			GetWarnCheckout().then((res: any) => {
				if (res.success) {
					state.tableData = res.data.list;
				}
			});

			GetEnvironmentList().then((res:any)=>{
				state.environmentList = res;
			});
		}

		initUser();
		initHomeCase();
		initHomeEvidence();
		initClass();
		initTrends();
		initWarning();
		return {
			getUserInfos,
			currentTime,
			tableRowClassName,
			goToPages,
			...toRefs(state),
		};
	},
};
</script>
<style >
.el-table .warning-row1{color:red !important;}
.el-table .row-hide{display:none;}
</style>
<style scoped>
/deep/ .el-card__header{
    font-size: 16px;
	font-weight: bold !important;

}
</style>
<style scoped lang="scss">
.homea {
	text-decoration: none;
}
.home-container {
	overflow-x: hidden;
	.home-card-item {
		width: 100%;
		height: 103px;
		background: var(--el-text-color-secondary);
		border-radius: 4px;
		transition: all ease 0.3s;
		&:hover {
			box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
			transition: all ease 0.3s;
		}
	}
	.home-card-item-box {
		display: flex;
		align-items: center;
		position: relative;
		overflow: hidden;
		&:hover {
			i {
				right: 0px !important;
				bottom: 0px !important;
				transition: all ease 0.3s;
			}
		}
		i {
			position: absolute;
			right: -10px;
			bottom: -10px;
			font-size: 70px;
			transform: rotate(-30deg);
			transition: all ease 0.3s;
		}
		.home-card-item-flex {
			padding: 0 20px;
			color: var(--color-whites);
			.home-card-item-title,
			.home-card-item-tip {
				font-size: 13px;
			}
			.home-card-item-title-num {
				font-size: 18px;
			}
			.home-card-item-tip-num {
				font-size: 13px;
			}
		}
	}
	.home-card-first {
		background: var(--el-color-white);
		border: 1px solid var(--el-border-color-light, #ebeef5);
		display: flex;
		align-items: center;
		img {
			width: 60px;
			height: 60px;
			border-radius: 100%;
			border: 2px solid var(--color-primary-light-5);
		}
		.home-card-first-right {
			flex: 1;
			display: flex;
			flex-direction: column;
			.home-card-first-right-title {
				color: var(--el-color-black);
			}
			.home-card-first-right-msg {
				font-size: 13px;
				color: var(--el-text-color-secondary);
			}
		}
	}
	.home-monitor {
		height: 200px;
		.flex-warp-item {
			width: 33%;
			height: 100px;
			display: flex;
			.flex-warp-item-box {
				margin: auto;
				height: auto;
				text-align: center;
				color: var(--el-text-color-primary);
			}
		}
	}
	.home-warning-card {
		height: 382px;
		::v-deep(.el-card) {
			height: 100%;
		}
	}
	.home-dynamic {
		height: 300px;
		.home-dynamic-item {
			display: flex;
			width: 100%;
			height: 60px;
			overflow: hidden;
			&:first-of-type {
				.home-dynamic-item-line {
					i {
						color: orange !important;
					}
				}
			}
			.home-dynamic-item-left {
				text-align: right;
				.home-dynamic-item-left-time1 {
				}
				.home-dynamic-item-left-time2 {
					font-size: 13px;
					color: var(--el-text-color-secondary);
				}
			}
			.home-dynamic-item-line {
				height: 60px;
				border-right: 2px dashed var(--el-border-color-light, #ebeef5);
				margin: 0 20px;
				position: relative;
				i {
					color: var(--color-primary);
					font-size: 12px;
					position: absolute;
					top: 1px;
					left: -6px;
					transform: rotate(46deg);
					background: var(--el-color-white);
				}
			}
			.home-dynamic-item-right {
				flex: 1;
				.home-dynamic-item-right-title {
					i {
						margin-right: 5px;
						border: 1px solid var(--el-border-color-light, #ebeef5);
						width: 20px;
						height: 20px;
						border-radius: 100%;
						padding: 3px 2px 2px;
						text-align: center;
						color: var(--color-primary);
					}
				}
				.home-dynamic-item-right-label {
					font-size: 13px;
					color: var(--el-text-color-secondary);
				}
			}
		}
	}
}


</style>
